package zbase;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {

    public static void main(String[] args) {

        try {
            Logger.getLogger("").addHandler(new FileHandler("errorlog.xml"));
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (args.length > 0 && args[0].equals("--install")) {
            FirstInstallDialog dlg = new FirstInstallDialog(null, true);
            dlg.setLocationRelativeTo(null);
            dlg.setVisible(true);
            if (!dlg.getResult().equals("Ok")) {
                return;
            }
        }

        // Properties
        Boolean NeedOptions = false;
        Vars.LoadProperties();
        if (!Vars.prop.containsKey("Username")) {
            Vars.prop.setProperty("Username", "zbase");
            NeedOptions = true;
        }
        if (!Vars.prop.containsKey("Userpass")) {
            Vars.prop.setProperty("Userpass", "zbase");
            NeedOptions = true;
        }
        if (!Vars.prop.containsKey("DB_URL")) {
            Vars.prop.setProperty("DB_URL", "jdbc:mysql://localhost:3306/zbase");
            NeedOptions = true;
        }
        if (!Vars.prop.containsKey("SortMode")) {
            Vars.prop.setProperty("SortMode", "znum");
        }
        if (!Vars.prop.containsKey("GroupMode")) {
            Vars.prop.setProperty("GroupMode", "default");
        }
        if (!Vars.prop.containsKey("ReportSignJob")) {
            Vars.prop.setProperty("ReportSignJob", "");
        }
        if (!Vars.prop.containsKey("ReportSignName")) {
            Vars.prop.setProperty("ReportSignName", "");
        }
        
        Boolean NeedDBInstall = false;
        int neenDBInstallCount = 0;
        DB db = new DB();
        if (db.executeNonQuery("SELECT k FROM zmain WHERE k = 0") != 0) {
            NeedDBInstall = true;
            neenDBInstallCount++;
        }
        if (db.executeNonQuery("SELECT k FROM objects WHERE k = 0") != 0) {
            NeedDBInstall = true;
            neenDBInstallCount++;
        }
        if (db.executeNonQuery("SELECT k FROM services WHERE k = 0") != 0) {
            NeedDBInstall = true;
            neenDBInstallCount++;
        }
        if (db.executeNonQuery("SELECT k FROM users WHERE k = 0") != 0) {
            NeedDBInstall = true;
            neenDBInstallCount++;
        }

        if (neenDBInstallCount == 4) {
            NeedOptions = true;
        } else {
            if (NeedDBInstall) {
                InstallDBDialog idbdlg = new InstallDBDialog(null, true);
                idbdlg.setLocationRelativeTo(null);
                idbdlg.setVisible(true);
                if (!idbdlg.getResult().equals("Ok")) {
                    return;
                }
            }
        }

        if (NeedOptions) {
            InstQDialog dlg = new InstQDialog(null, true);
            dlg.setLocationRelativeTo(null);
            dlg.setVisible(true);
            if (dlg.getResult().equals("ExitPressed") || dlg.getResult().equals("False")) {
                return;
            }
            if (dlg.getResult().equals("CreateNew")) {
                FirstInstallDialog dlgcn = new FirstInstallDialog(null, true);
                dlgcn.setLocationRelativeTo(null);
                dlgcn.setVisible(true);
                if (!dlgcn.getResult().equals("Ok")) {
                return;
            }
            }
            if (dlg.getResult().equals("Connect")) {
                OptionsDialog dlgo = new OptionsDialog(null, true);
                dlgo.setLocationRelativeTo(null);
                dlgo.setVisible(true);
                if (!dlgo.getResult().equals("Ok")) {
                    return;
                }
            }
        }
        // End of Properties

        LoginDialog ldlg = new LoginDialog(null, true);
        ldlg.setLocationRelativeTo(null);
        ldlg.setVisible(true);
        if (!ldlg.result.equals("Ok")) {
            return;
        }

        MainFrame frm = new MainFrame();
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }
}
